package interview.huawei.tjt.training.huisu;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。
 *
 * 17.电话号码的字母组合
 *
 * 示例: 输入："23" 输出：["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
 *
 * 说明：尽管上面的答案是按字典序排列的，但是你可以任意选择答案输出的顺序。
 */
public class DianHuaHaoMaHeZiMu {

    private static List<String> result = new ArrayList<>();
    private static String[] numString = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    private static StringBuffer stringBuffer = new StringBuffer();

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            String digit = sc.nextLine();
            helper(digit, numString, 0);
            System.out.println(result);
            result.clear();
        }
    }

    private static void helper (String digit, String[] numString,int num) {
        //终止条件
        if (num == digit.length()) {
            result.add(stringBuffer.toString());
            return;
        }
        String str = numString[Integer.parseInt(String.valueOf(digit.charAt(num)))];

        for (int i = 0; i < str.length(); i ++) {
            stringBuffer.append(str.charAt(i));
            helper(digit, numString, num + 1);
            // 回溯
            stringBuffer.deleteCharAt(stringBuffer.length() - 1);
        }
    }

}
